package com.linchaoen;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * 管道类
 */
public class Column {

    public BufferedImage columnImage;

    public int x;

    public int y;

    public int width;

    public int height;

    public int distance = 270;

    //间隙
    public int gap = 144;

    //必须静态，共享变量
    public static int count = 0;

    public Random random = new Random();

    public Column() {
        try {
            columnImage = ImageIO.read(getClass().getResource("column.png"));
            width = columnImage.getWidth();//从图片中直接读取宽度
            height = columnImage.getHeight();//从图片中直接读取高度
            //x=415
            x = 415 + distance * count;//两根柱子出现的间距
            //y = -200;
            //random+100是管道之间空隙坐标，而y是管道最顶点坐标，所以要减去height/2(往上移所以是减)
            y = random.nextInt(300) + 100 - height / 2;
        } catch (IOException e) {
            e.printStackTrace();
        }
        count++;
    }

    /**
     * 管道动画
     */
    public void step() {
        x--;
        if (x == -78) {
            x = 415;
            y = random.nextInt(300) + 100 - height / 2;
        }
    }

}
